package cn.csg.iotplatform.module.system.controller.admin.linktest;

import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.module.system.dal.dataobject.linktest.LinkProxyConfigDO;
import cn.csg.iotplatform.module.system.dal.mysql.linktest.LinkTestMapper;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.List;

@Tag(name = "管理后台 - Test")
@RestController
@RequestMapping("/system/link-test")
@Validated
@Slf4j
public class LinkTestController {

    @Resource
    private LinkTestMapper linkTestMapper;

    @Resource
    private KafkaTemplate kafkaTemplate;

    @Value("${link_test.topic}")
    private String topic;

    @PostMapping("/testAll")
    @Operation(summary = "链路测试全部")
    @PermitAll
    public CommonResult<Integer> linkTestAll() {
       List<LinkProxyConfigDO> list = linkTestMapper.selectList();
       for(LinkProxyConfigDO proxyConfigDO : list){
           long timestamp = System.currentTimeMillis();
           JSONObject jsonObject = new JSONObject();
           jsonObject.put("taskId", "LINK_"+timestamp);
           jsonObject.put("startTimestamp",timestamp);
           jsonObject.put("zone",proxyConfigDO.getZone());
           jsonObject.put("node",proxyConfigDO.getNode());
           HttpRequest httpRequest = HttpUtil.createPost(proxyConfigDO.getProxyUrl());
           httpRequest.header("link_test","true");
           String result = httpRequest.timeout(60*1000).body(jsonObject.toJSONString()).execute().body();
           log.info("linkTest>>>result={}",result);
           jsonObject = new JSONObject();
           jsonObject.put("taskId", "LINK_"+timestamp);
           jsonObject.put("startTimestamp",timestamp);
           jsonObject.put("currentTimestamp",System.currentTimeMillis());
           jsonObject.put("sort",0);
           kafkaTemplate.send(new ProducerRecord<>(String.format(topic,proxyConfigDO.getConfigId()), jsonObject.toJSONString()));
       }
        return CommonResult.success(1);
    }

    @PostMapping("/test")
    @Operation(summary = "链路测试单个")
    @PermitAll
    public CommonResult<Integer> linkTest(LinkProxyConfigDO proxyConfigDO) {
        LambdaQueryWrapper<LinkProxyConfigDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LinkProxyConfigDO::getConfigId,proxyConfigDO.getConfigId());
        proxyConfigDO = linkTestMapper.selectOne(wrapper);
        long timestamp = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", "LINK_"+timestamp);
        jsonObject.put("startTimestamp",timestamp);
        jsonObject.put("zone",proxyConfigDO.getZone());
        jsonObject.put("node",proxyConfigDO.getNode());
        HttpRequest httpRequest = HttpUtil.createPost(proxyConfigDO.getProxyUrl());
        httpRequest.header("link_test","true");
        String result = httpRequest.timeout(60*1000).body(jsonObject.toJSONString()).execute().body();
        log.info("linkTest>>>result={}",result);
        jsonObject = new JSONObject();
        jsonObject.put("taskId", "LINK_"+timestamp);
        jsonObject.put("startTimestamp",timestamp);
        jsonObject.put("currentTimestamp",System.currentTimeMillis());
        jsonObject.put("sort",0);
        kafkaTemplate.send(new ProducerRecord<>(String.format(topic,proxyConfigDO.getConfigId()), jsonObject.toJSONString()));
        return CommonResult.success(1);
    }

    @GetMapping("/list")
    @Operation(summary = "链路列表")
    @PermitAll
    public CommonResult<List<LinkProxyConfigDO>> linkList() {
        return CommonResult.success(linkTestMapper.selectList());
    }

}
